package com.wkk.nursinghome.convert;

import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl;

import java.util.Calendar;

public class Age {
    /**
     * 根据出生日期获取人的年龄
     *
     * @param strBirthDate(yyyy-mm-dd or yyyy/mm/dd)
     * @return
     */
    public static String getPersonAgeByBirthDate(String strBirthDate){

        if ("".equals(strBirthDate) || strBirthDate == null){
            return "";
        }
        //读取当前日期
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH)+1;
        int day = c.get(Calendar.DATE);
        //计算年龄
        int age = year - Integer.parseInt(strBirthDate.substring(0, 4)) - 1;
        if (Integer.parseInt(strBirthDate.substring(5,7)) < month) {
            age++;
        } else if (Integer.parseInt(strBirthDate.substring(5,7))== month && Integer.parseInt(strBirthDate.substring(8,10)) <= day){
            age++;
        }
        return String.valueOf(age);
    }

    public static void main(String[] args) {

        System.out.println(getPersonAgeByBirthDate("1990-02-02"));

    }

}
